package ru.myrtle.umf.tools.ugen.templates;

import java.io.*;
import java.text.*;
import java.util.*;
import ru.myrtle.umf.*;
import ru.myrtle.umf.codegen.*;
import ru.myrtle.umf.codegen.java.*;
import ru.myrtle.umf.tools.ugen.*;

public class UObjectImplTemplate extends UTemplate
{
    protected final String NL = System.getProperties().getProperty("line.separator");
    protected final String TEXT_1 = "package ";
    protected final String TEXT_2 = ";" + NL + NL + "public class ";
    protected final String TEXT_3 = " extends ";
    protected final String TEXT_4 = " implements ";
    protected final String TEXT_5 = " {" + NL + NL;
    protected final String TEXT_6 = "    protected static final ";
    protected final String TEXT_7 = " ";
    protected final String TEXT_8 = " = ";
    protected final String TEXT_9 = ";" + NL;
    protected final String TEXT_10 = NL + "    public ";
    protected final String TEXT_11 = "() {" + NL + "    }" + NL + NL + "    @Override" + NL + "    public UClass uClass() {" + NL + "        return ";
    protected final String TEXT_12 = ".";
    protected final String TEXT_13 = ";" + NL + "    }" + NL + "    " + NL;
    protected final String TEXT_14 = "    @Override" + NL + "    public final ";
    protected final String TEXT_15 = " ";
    protected final String TEXT_16 = "() {" + NL + "        return ";
    protected final String TEXT_17 = ".INSTANCE.";
    protected final String TEXT_18 = "(this);" + NL + "    }" + NL + NL + "    @Override" + NL + "    public final boolean ";
    protected final String TEXT_19 = "() {" + NL + "        return ";
    protected final String TEXT_20 = ".INSTANCE.";
    protected final String TEXT_21 = "(this);" + NL + "    }" + NL + NL;
    protected final String TEXT_22 = "    @Override" + NL + "    public final void ";
    protected final String TEXT_23 = "(";
    protected final String TEXT_24 = " ";
    protected final String TEXT_25 = ") {" + NL + "        ";
    protected final String TEXT_26 = ".INSTANCE.";
    protected final String TEXT_27 = "(this, ";
    protected final String TEXT_28 = ");" + NL + "    }" + NL + NL + "    @Override" + NL + "    public final void ";
    protected final String TEXT_29 = "() {" + NL + "        ";
    protected final String TEXT_30 = ".INSTANCE.";
    protected final String TEXT_31 = "(this);" + NL + "    }" + NL + "    " + NL;
    protected final String TEXT_32 = "    @Override" + NL + "    public final ";
    protected final String TEXT_33 = " ";
    protected final String TEXT_34 = "() {" + NL + "        return ";
    protected final String TEXT_35 = ".INSTANCE.";
    protected final String TEXT_36 = "(this);" + NL + "    }" + NL + NL + "    @Override" + NL + "    public final boolean ";
    protected final String TEXT_37 = "() {" + NL + "        return ";
    protected final String TEXT_38 = ".INSTANCE.";
    protected final String TEXT_39 = "(this);" + NL + "    }" + NL + NL;
    protected final String TEXT_40 = "    @Override" + NL + "    public final void ";
    protected final String TEXT_41 = "(";
    protected final String TEXT_42 = " ";
    protected final String TEXT_43 = ") {" + NL + "        ";
    protected final String TEXT_44 = ".INSTANCE.";
    protected final String TEXT_45 = "(this, ";
    protected final String TEXT_46 = ");" + NL + "    }" + NL + NL + "    @Override" + NL + "    public final void ";
    protected final String TEXT_47 = "() {" + NL + "        ";
    protected final String TEXT_48 = ".INSTANCE.";
    protected final String TEXT_49 = "(this);" + NL + "    }" + NL + NL;
    protected final String TEXT_50 = "    private ";
    protected final String TEXT_51 = " ";
    protected final String TEXT_52 = " = ";
    protected final String TEXT_53 = ";" + NL + NL + "    @Override" + NL + "    public final ";
    protected final String TEXT_54 = " ";
    protected final String TEXT_55 = "() {" + NL + "        return ";
    protected final String TEXT_56 = ";" + NL + "    }" + NL + NL + "    @Override" + NL + "    public final boolean isSet";
    protected final String TEXT_57 = "() {" + NL + "        return ";
    protected final String TEXT_58 = " != ";
    protected final String TEXT_59 = ";" + NL + "    }" + NL + NL;
    protected final String TEXT_60 = "    @Override" + NL + "    public final void set";
    protected final String TEXT_61 = "(";
    protected final String TEXT_62 = " ";
    protected final String TEXT_63 = ") {" + NL + "        this.";
    protected final String TEXT_64 = " = ";
    protected final String TEXT_65 = ";" + NL + "    }" + NL + NL + "    @Override" + NL + "    public final void unset";
    protected final String TEXT_66 = "() {" + NL + "        ";
    protected final String TEXT_67 = " = ";
    protected final String TEXT_68 = ";" + NL + "    }" + NL + NL;
    protected final String TEXT_69 = "    private ";
    protected final String TEXT_70 = " ";
    protected final String TEXT_71 = ";" + NL + NL + "    @Override" + NL + "    public final ";
    protected final String TEXT_72 = " get";
    protected final String TEXT_73 = "() {" + NL;
    protected final String TEXT_74 = "        if (";
    protected final String TEXT_75 = " == null) {" + NL + "            ";
    protected final String TEXT_76 = " = new UObjectContainmentWithInverseList";
    protected final String TEXT_77 = "<";
    protected final String TEXT_78 = ">(this, ";
    protected final String TEXT_79 = ".";
    protected final String TEXT_80 = ", ";
    protected final String TEXT_81 = ".";
    protected final String TEXT_82 = ");" + NL + "        }" + NL;
    protected final String TEXT_83 = "        if (";
    protected final String TEXT_84 = " == null) {" + NL + "            ";
    protected final String TEXT_85 = " = new UObjectContainmentList";
    protected final String TEXT_86 = "<";
    protected final String TEXT_87 = ">(this, ";
    protected final String TEXT_88 = ".";
    protected final String TEXT_89 = ");" + NL + "        }" + NL;
    protected final String TEXT_90 = "        if (";
    protected final String TEXT_91 = " == null) {" + NL + "            ";
    protected final String TEXT_92 = " = new UObjectWithInverseList";
    protected final String TEXT_93 = "<";
    protected final String TEXT_94 = ">(this, ";
    protected final String TEXT_95 = ".";
    protected final String TEXT_96 = ");" + NL + "        }" + NL;
    protected final String TEXT_97 = "        if (";
    protected final String TEXT_98 = " == null) {" + NL + "            ";
    protected final String TEXT_99 = " = new UObjectList";
    protected final String TEXT_100 = "<";
    protected final String TEXT_101 = ">();" + NL + "        }" + NL;
    protected final String TEXT_102 = "        return ";
    protected final String TEXT_103 = ";" + NL + "    }" + NL + NL + "    @Override" + NL + "    public final boolean isSet";
    protected final String TEXT_104 = "() {" + NL + "        return ";
    protected final String TEXT_105 = " != null && !";
    protected final String TEXT_106 = ".isEmpty();" + NL + "    }" + NL + NL;
    protected final String TEXT_107 = "    @Override" + NL + "    public void set";
    protected final String TEXT_108 = "(";
    protected final String TEXT_109 = " ";
    protected final String TEXT_110 = ") {" + NL + "        ";
    protected final String TEXT_111 = "().clear();" + NL + "        ";
    protected final String TEXT_112 = "().addAll(";
    protected final String TEXT_113 = ");" + NL + "    }" + NL + NL + "    @Override" + NL + "    public void unset";
    protected final String TEXT_114 = "() {" + NL + "        if (";
    protected final String TEXT_115 = " != null) {" + NL + "            ";
    protected final String TEXT_116 = ".clear();" + NL + "        }" + NL + "    }" + NL + NL;
    protected final String TEXT_117 = "    private ";
    protected final String TEXT_118 = " ";
    protected final String TEXT_119 = ";" + NL + NL + "    @Override" + NL + "    public final ";
    protected final String TEXT_120 = " get";
    protected final String TEXT_121 = "() {" + NL + "        return ";
    protected final String TEXT_122 = ";" + NL + "    }" + NL + NL + "    @Override" + NL + "    public final boolean isSet";
    protected final String TEXT_123 = "() {" + NL + "        return ";
    protected final String TEXT_124 = " != null;" + NL + "    }" + NL + NL + "    private void set";
    protected final String TEXT_125 = "Impl(";
    protected final String TEXT_126 = " ";
    protected final String TEXT_127 = ") {" + NL + "        this.";
    protected final String TEXT_128 = " = ";
    protected final String TEXT_129 = ";" + NL + "    }" + NL + NL;
    protected final String TEXT_130 = "    @Override" + NL + "    public final void set";
    protected final String TEXT_131 = "(";
    protected final String TEXT_132 = " ";
    protected final String TEXT_133 = ") {" + NL;
    protected final String TEXT_134 = "        if (this.";
    protected final String TEXT_135 = " != ";
    protected final String TEXT_136 = ") {" + NL + "            ";
    protected final String TEXT_137 = "if (this.";
    protected final String TEXT_138 = " != null) {";
    protected final String TEXT_139 = "                ";
    protected final String TEXT_140 = "this.";
    protected final String TEXT_141 = ".uInternalDetach();";
    protected final String TEXT_142 = "                ";
    protected final String TEXT_143 = "this.";
    protected final String TEXT_144 = ".uInternalInverseRemove(this, ";
    protected final String TEXT_145 = ".";
    protected final String TEXT_146 = ");";
    protected final String TEXT_147 = "            ";
    protected final String TEXT_148 = "}";
    protected final String TEXT_149 = "            set";
    protected final String TEXT_150 = "Impl(";
    protected final String TEXT_151 = ");" + NL + "            ";
    protected final String TEXT_152 = "if (this.";
    protected final String TEXT_153 = " != null) {";
    protected final String TEXT_154 = "                ";
    protected final String TEXT_155 = "this.";
    protected final String TEXT_156 = ".uInternalAttach(this, ";
    protected final String TEXT_157 = ".";
    protected final String TEXT_158 = ");";
    protected final String TEXT_159 = "                ";
    protected final String TEXT_160 = "this.";
    protected final String TEXT_161 = ".uInternalInverseAdd(this, ";
    protected final String TEXT_162 = ".";
    protected final String TEXT_163 = ");";
    protected final String TEXT_164 = "            ";
    protected final String TEXT_165 = "}";
    protected final String TEXT_166 = "        }" + NL + "    }" + NL + "    " + NL + "    @Override" + NL + "    public final void unset";
    protected final String TEXT_167 = "() {" + NL + "        set";
    protected final String TEXT_168 = "Impl(null);" + NL + "    }" + NL + NL;
    protected final String TEXT_169 = NL + "    @Override" + NL + "    @SuppressWarnings(" + NL + "        \"unchecked\"" + NL + "    )" + NL + "    public Object uGet(UFeature feature) {" + NL;
    protected final String TEXT_170 = "        if (feature == ";
    protected final String TEXT_171 = ".";
    protected final String TEXT_172 = ") {" + NL + "            return ";
    protected final String TEXT_173 = "();" + NL + "        }" + NL;
    protected final String TEXT_174 = "        if (feature == ";
    protected final String TEXT_175 = ".";
    protected final String TEXT_176 = ") {" + NL + "            return get";
    protected final String TEXT_177 = "();" + NL + "        }" + NL;
    protected final String TEXT_178 = NL + "        return super.uGet(feature);" + NL + "    }" + NL + NL + "    @Override" + NL + "    @SuppressWarnings(" + NL + "        \"unchecked\"" + NL + "    )" + NL + "    public boolean uIsSet(UFeature feature) {" + NL;
    protected final String TEXT_179 = "        if (feature == ";
    protected final String TEXT_180 = ".";
    protected final String TEXT_181 = ") {" + NL + "            return isSet";
    protected final String TEXT_182 = "();" + NL + "        }" + NL;
    protected final String TEXT_183 = "        if (feature == ";
    protected final String TEXT_184 = ".";
    protected final String TEXT_185 = ") {" + NL + "            return isSet";
    protected final String TEXT_186 = "();" + NL + "        }" + NL;
    protected final String TEXT_187 = NL + "        return super.uIsSet(feature);" + NL + "    }" + NL + NL + "    @Override" + NL + "    @SuppressWarnings(" + NL + "        \"unchecked\"" + NL + "    )" + NL + "    public void uSet(UFeature feature, Object value) {" + NL;
    protected final String TEXT_188 = "        if (feature == ";
    protected final String TEXT_189 = ".";
    protected final String TEXT_190 = ") {" + NL + "            set";
    protected final String TEXT_191 = "(";
    protected final String TEXT_192 = ");" + NL + "            return;" + NL + "        }" + NL;
    protected final String TEXT_193 = "        if (feature == ";
    protected final String TEXT_194 = ".";
    protected final String TEXT_195 = ") {" + NL + "            set";
    protected final String TEXT_196 = "(";
    protected final String TEXT_197 = ");" + NL + "            return;" + NL + "        }" + NL;
    protected final String TEXT_198 = NL + "        super.uSet(feature, value);" + NL + "    }" + NL + NL + "    @Override" + NL + "    @SuppressWarnings(" + NL + "        \"unchecked\"" + NL + "    )" + NL + "    public void uUnset(UFeature feature) {" + NL;
    protected final String TEXT_199 = "        if (feature == ";
    protected final String TEXT_200 = ".";
    protected final String TEXT_201 = ") {" + NL + "            unset";
    protected final String TEXT_202 = "();" + NL + "            return;" + NL + "        }" + NL;
    protected final String TEXT_203 = "        if (feature == ";
    protected final String TEXT_204 = ".";
    protected final String TEXT_205 = ") {" + NL + "            unset";
    protected final String TEXT_206 = "();" + NL + "            return;" + NL + "        }" + NL;
    protected final String TEXT_207 = NL + "        super.uUnset(feature);" + NL + "    }" + NL + NL;
    protected final String TEXT_208 = NL + "    @Override" + NL + "    @SuppressWarnings(" + NL + "        \"unchecked\"" + NL + "    )" + NL + "    public void uInternalInverseAdd(UObject otherEnd, UReference reference) {" + NL;
    protected final String TEXT_209 = "        if (reference == ";
    protected final String TEXT_210 = ".";
    protected final String TEXT_211 = ") {" + NL;
    protected final String TEXT_212 = "            ((UObjectList";
    protected final String TEXT_213 = "<";
    protected final String TEXT_214 = ">) ";
    protected final String TEXT_215 = "()).basicAdd((";
    protected final String TEXT_216 = ") otherEnd);" + NL + "            return;" + NL;
    protected final String TEXT_217 = "            set";
    protected final String TEXT_218 = "Impl((";
    protected final String TEXT_219 = ") otherEnd);" + NL + "            return;" + NL;
    protected final String TEXT_220 = "        }" + NL;
    protected final String TEXT_221 = "    }" + NL + NL + "    @Override" + NL + "    @SuppressWarnings(" + NL + "        \"unchecked\"" + NL + "    )" + NL + "    public void uInternalInverseRemove(UObject otherEnd, UReference reference) {" + NL;
    protected final String TEXT_222 = "        if (reference == ";
    protected final String TEXT_223 = ".";
    protected final String TEXT_224 = ") {" + NL;
    protected final String TEXT_225 = "            ((UObjectList";
    protected final String TEXT_226 = "<";
    protected final String TEXT_227 = ">) ";
    protected final String TEXT_228 = "()).basicRemove((";
    protected final String TEXT_229 = ") otherEnd);" + NL + "            return;" + NL;
    protected final String TEXT_230 = "            set";
    protected final String TEXT_231 = "Impl(null);" + NL + "            return;" + NL;
    protected final String TEXT_232 = "        }" + NL;
    protected final String TEXT_233 = "    }" + NL + NL;
    protected final String TEXT_234 = NL;
    protected final String TEXT_235 = "    @Override" + NL + "    ";
    protected final String TEXT_236 = " {" + NL + "        ";
    protected final String TEXT_237 = ".INSTANCE.";
    protected final String TEXT_238 = "(";
    protected final String TEXT_239 = ");" + NL + "    }" + NL + NL;
    protected final String TEXT_240 = "    @Override" + NL + "    public String toString() {" + NL + "        StringBuffer result = new StringBuffer();" + NL + NL + "        result.append(\"";
    protected final String TEXT_241 = "\");" + NL + "        result.append(\" (\");" + NL;
    protected final String TEXT_242 = "        result.append(\"";
    protected final String TEXT_243 = ": \");" + NL + "        result.append(";
    protected final String TEXT_244 = ");" + NL;
    protected final String TEXT_245 = "        result.append(\", \");" + NL;
    protected final String TEXT_246 = "        result.append(')');" + NL + NL + "        return result.toString();" + NL + "    }" + NL + "}" + NL;
    protected final String TEXT_247 = NL + NL + NL + NL;

    /*
     *
     */


    public UObjectImplTemplate() {

    }

    /*
     *
     */

    public String generate(UTemplateContext context) throws UTemplateRejectException
    {
        final StringBuffer stringBuffer = new StringBuffer();

    checkTemplateObjectType(context, UClass.class);

    UClass object = (UClass) context.getObject();

    if (object.isVirtual()) {
        reject();
    }
    if (object.isProvided()) {
        reject();
    }

    String implName    = UGenUtil.getObjectImplClassName(object);
    String implPackage = UGenUtil.getObjectImplClassPackage(object);

    String modelClassName     = UGenUtil.getModelClassName(object.getPackage());
    String modelClassPackage  = UGenUtil.getModelClassPackage(object.getPackage());

    String classLiteral = UGenUtil.getClassifierLiteral(object);

    context.getResource().setObjectFileName(implName);
    context.getResource().setObjectFileExtension("java");
    context.getResource().setObjectPackage(implPackage);

    JavaImportBuilder importBuilder = new JavaImportBuilder();

    importBuilder.addPackage("java.util");
    importBuilder.addPackage("ru.myrtle.umf");
    importBuilder.addPackage("ru.myrtle.umf.util");
    importBuilder.addPackage(modelClassPackage);

    String baseClass = UGenUtil.getBaseClassName(
        context, importBuilder
    );

        stringBuffer.append(TEXT_1);
        stringBuffer.append(implPackage);
        stringBuffer.append(TEXT_2);
        stringBuffer.append(implName);
        stringBuffer.append(TEXT_3);
        stringBuffer.append(baseClass);
        stringBuffer.append(TEXT_4);
        stringBuffer.append(object.getName());
        stringBuffer.append(TEXT_5);

    for (UAttribute attribute : object.getAllAttributes()) {
        if (attribute.isDerived() || attribute.isVolatile()) {
            continue;
        }

        String attributeType = UGenUtil.getInstanceTypeName(
            attribute, importBuilder
        );

        String literal = UGenUtil.getDefaultValueLiteral(attribute);
        String value = UGenUtil.getDefaultValueAsString(
            attribute.getType()
        );

        stringBuffer.append(TEXT_6);
        stringBuffer.append(attributeType);
        stringBuffer.append(TEXT_7);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_8);
        stringBuffer.append(value);
        stringBuffer.append(TEXT_9);

    }

        stringBuffer.append(TEXT_10);
        stringBuffer.append(implName);
        stringBuffer.append(TEXT_11);
        stringBuffer.append(modelClassName);
        stringBuffer.append(TEXT_12);
        stringBuffer.append(classLiteral);
        stringBuffer.append(TEXT_13);

    for (UFeature feature : object.getAllFeatures()) {
        if (feature.isDerived()) {
            String opClassName = UGenUtil.getObjectOpClassName(object);

            if (feature instanceof UAttribute) {
                UAttribute attribute = (UAttribute) feature;

                String attributeName = attribute.getName();
                String attributeType = UGenUtil.getInstanceTypeName(
                    attribute, importBuilder
                );

                String varName = "_" + attributeName + "_";

                String getOpName   = UGenUtil.getAccessorPrefix(attribute) + UGenUtil.capName(attributeName);
                String isSetOpName = "isSet" + UGenUtil.capName(attributeName);

        stringBuffer.append(TEXT_14);
        stringBuffer.append(attributeType);
        stringBuffer.append(TEXT_15);
        stringBuffer.append(getOpName);
        stringBuffer.append(TEXT_16);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_17);
        stringBuffer.append(getOpName);
        stringBuffer.append(TEXT_18);
        stringBuffer.append(isSetOpName);
        stringBuffer.append(TEXT_19);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_20);
        stringBuffer.append(isSetOpName);
        stringBuffer.append(TEXT_21);

                if (attribute.isChangeable()) {
                    String setOpName    = "set" + UGenUtil.capName(attributeName);
                    String unsetOpName  = "unset" + UGenUtil.capName(attributeName);

        stringBuffer.append(TEXT_22);
        stringBuffer.append(setOpName);
        stringBuffer.append(TEXT_23);
        stringBuffer.append(attributeType);
        stringBuffer.append(TEXT_24);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_25);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_26);
        stringBuffer.append(setOpName);
        stringBuffer.append(TEXT_27);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_28);
        stringBuffer.append(unsetOpName);
        stringBuffer.append(TEXT_29);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_30);
        stringBuffer.append(unsetOpName);
        stringBuffer.append(TEXT_31);

                }
            }
            else if (feature instanceof UReference) {
                UReference reference = (UReference) feature;

                String referenceName = reference.getName();
                String referenceType = UGenUtil.getInstanceTypeName(
                    reference, importBuilder
                );

                String varName = "_" + referenceName + "_";

                String getOpName   = "get" + UGenUtil.capName(referenceName);
                String isSetOpName = "isSet" + UGenUtil.capName(referenceName);

        stringBuffer.append(TEXT_32);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_33);
        stringBuffer.append(getOpName);
        stringBuffer.append(TEXT_34);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_35);
        stringBuffer.append(getOpName);
        stringBuffer.append(TEXT_36);
        stringBuffer.append(isSetOpName);
        stringBuffer.append(TEXT_37);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_38);
        stringBuffer.append(isSetOpName);
        stringBuffer.append(TEXT_39);

                    if (reference.isChangeable()) {
                        String setOpName       = "set" + UGenUtil.capName(referenceName);
                        String unsetOpName  = "unset" + UGenUtil.capName(referenceName);


        stringBuffer.append(TEXT_40);
        stringBuffer.append(setOpName);
        stringBuffer.append(TEXT_41);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_42);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_43);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_44);
        stringBuffer.append(setOpName);
        stringBuffer.append(TEXT_45);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_46);
        stringBuffer.append(unsetOpName);
        stringBuffer.append(TEXT_47);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_48);
        stringBuffer.append(unsetOpName);
        stringBuffer.append(TEXT_49);

                    }
            }
        }
        else {
            if (feature instanceof UAttribute) {
                UAttribute attribute = (UAttribute) feature;

                String attributeName = attribute.getName();
                String attributeType = UGenUtil.getInstanceTypeName(
                    attribute, importBuilder
                );
                String defaultValueLiteral = UGenUtil.getDefaultValueLiteral(attribute);

                String varName = "_" + attributeName + "_";

        stringBuffer.append(TEXT_50);
        stringBuffer.append(attributeType);
        stringBuffer.append(TEXT_51);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_52);
        stringBuffer.append(defaultValueLiteral);
        stringBuffer.append(TEXT_53);
        stringBuffer.append(attributeType);
        stringBuffer.append(TEXT_54);
        stringBuffer.append(UGenUtil.getAccessorPrefix(attribute));
        stringBuffer.append(UGenUtil.capName(attributeName));
        stringBuffer.append(TEXT_55);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_56);
        stringBuffer.append(UGenUtil.capName(attributeName));
        stringBuffer.append(TEXT_57);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_58);
        stringBuffer.append(defaultValueLiteral);
        stringBuffer.append(TEXT_59);

                if (attribute.isChangeable()) {

        stringBuffer.append(TEXT_60);
        stringBuffer.append(UGenUtil.capName(attributeName));
        stringBuffer.append(TEXT_61);
        stringBuffer.append(attributeType);
        stringBuffer.append(TEXT_62);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_63);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_64);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_65);
        stringBuffer.append(UGenUtil.capName(attributeName));
        stringBuffer.append(TEXT_66);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_67);
        stringBuffer.append(defaultValueLiteral);
        stringBuffer.append(TEXT_68);

                }
            }
            else if (feature instanceof UReference) {
                UReference reference = (UReference) feature;

                String referenceName = reference.getName();
                String referenceType = UGenUtil.getInstanceTypeName(
                    reference, importBuilder
                );

                String elementType = UGenUtil.getInstanceTypeName(
                    reference.getType(), importBuilder
                );

                String varName = "_" + referenceName + "_";

                importBuilder.addPackage(
                    UGenUtil.getObjectClassPackage(
                        reference.getType()
                    )
                );

                String referenceLiteral = UGenUtil.getFeatureLiteral(reference);

                String oppositeModel   = "";
                String oppositeLiteral = "";

                UReference opposite = reference.getOpposite();

                if (opposite != null) {
                    UClass oppositeContainer = opposite.getContainingClass();

                    oppositeModel = UGenUtil.getModelClassName(
                        oppositeContainer.getPackage()
                    );
                    oppositeLiteral = UGenUtil.getFeatureLiteral(opposite);
                }

                if (reference.isMany()) {
                    String getter = "get" + UGenUtil.capName(referenceName);

        stringBuffer.append(TEXT_69);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_70);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_71);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_72);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_73);

                    if (reference.isContainment() && reference.getOpposite() != null) {

        stringBuffer.append(TEXT_74);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_75);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_76);
        stringBuffer.append(TEXT_77);
        stringBuffer.append(elementType);
        stringBuffer.append(TEXT_78);
        stringBuffer.append(modelClassName);
        stringBuffer.append(TEXT_79);
        stringBuffer.append(referenceLiteral);
        stringBuffer.append(TEXT_80);
        stringBuffer.append(oppositeModel);
        stringBuffer.append(TEXT_81);
        stringBuffer.append(oppositeLiteral);
        stringBuffer.append(TEXT_82);

                    }
                    else if (reference.isContainment()) {

        stringBuffer.append(TEXT_83);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_84);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_85);
        stringBuffer.append(TEXT_86);
        stringBuffer.append(elementType);
        stringBuffer.append(TEXT_87);
        stringBuffer.append(modelClassName);
        stringBuffer.append(TEXT_88);
        stringBuffer.append(referenceLiteral);
        stringBuffer.append(TEXT_89);

                    }
                    else if (reference.getOpposite() != null) {

        stringBuffer.append(TEXT_90);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_91);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_92);
        stringBuffer.append(TEXT_93);
        stringBuffer.append(elementType);
        stringBuffer.append(TEXT_94);
        stringBuffer.append(oppositeModel);
        stringBuffer.append(TEXT_95);
        stringBuffer.append(oppositeLiteral);
        stringBuffer.append(TEXT_96);

                    }
                    else {

        stringBuffer.append(TEXT_97);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_98);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_99);
        stringBuffer.append(TEXT_100);
        stringBuffer.append(elementType);
        stringBuffer.append(TEXT_101);

                    }

        stringBuffer.append(TEXT_102);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_103);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_104);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_105);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_106);

                    if (reference.isChangeable()) {

        stringBuffer.append(TEXT_107);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_108);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_109);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_110);
        stringBuffer.append(getter);
        stringBuffer.append(TEXT_111);
        stringBuffer.append(getter);
        stringBuffer.append(TEXT_112);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_113);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_114);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_115);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_116);

                    }
                }
                else {

        stringBuffer.append(TEXT_117);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_118);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_119);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_120);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_121);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_122);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_123);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_124);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_125);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_126);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_127);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_128);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_129);

                    if (reference.isChangeable()) {

        stringBuffer.append(TEXT_130);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_131);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_132);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_133);

        boolean processing = reference.isContainment() || reference.getOpposite() != null;

        stringBuffer.append(TEXT_134);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_135);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_136);
if (processing) {
        stringBuffer.append(TEXT_137);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_138);
}
        stringBuffer.append(TEXT_139);
if (reference.isContainment()) {
        stringBuffer.append(TEXT_140);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_141);
}
        stringBuffer.append(TEXT_142);
if (reference.getOpposite() != null) {
        stringBuffer.append(TEXT_143);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_144);
        stringBuffer.append(oppositeModel);
        stringBuffer.append(TEXT_145);
        stringBuffer.append(oppositeLiteral);
        stringBuffer.append(TEXT_146);
}
        stringBuffer.append(TEXT_147);
if (processing){
        stringBuffer.append(TEXT_148);
}
        stringBuffer.append(TEXT_149);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_150);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_151);
if (processing) {
        stringBuffer.append(TEXT_152);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_153);
}
        stringBuffer.append(TEXT_154);
if (reference.isContainment()) {
        stringBuffer.append(TEXT_155);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_156);
        stringBuffer.append(modelClassName);
        stringBuffer.append(TEXT_157);
        stringBuffer.append(referenceLiteral);
        stringBuffer.append(TEXT_158);
}
        stringBuffer.append(TEXT_159);
if (reference.getOpposite() != null) {
        stringBuffer.append(TEXT_160);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_161);
        stringBuffer.append(oppositeModel);
        stringBuffer.append(TEXT_162);
        stringBuffer.append(oppositeLiteral);
        stringBuffer.append(TEXT_163);
}
        stringBuffer.append(TEXT_164);
if (processing){
        stringBuffer.append(TEXT_165);
}
        stringBuffer.append(TEXT_166);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_167);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_168);

                    }
                }
            }
        }
    }

        stringBuffer.append(TEXT_169);

    for (UFeature feature : object.getAllFeatures()) {
        if (feature instanceof UAttribute) {
            UAttribute attribute          = (UAttribute) feature;
            UClass     attributeContainer = attribute.getContainingClass();

            String attributeModelName = attributeContainer.getPackage().getName();

            importBuilder.addPackage(
                UGenUtil.getObjectClassPackage(
                    attributeContainer
                )
            );

            String attributeName = attribute.getName();
            String literal = UGenUtil.getFeatureLiteral(attribute);

            String accessorPrefix = UGenUtil.getAccessorPrefix(attribute);;

        stringBuffer.append(TEXT_170);
        stringBuffer.append(attributeModelName);
        stringBuffer.append(TEXT_171);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_172);
        stringBuffer.append(accessorPrefix);
        stringBuffer.append(UGenUtil.capName(attributeName));
        stringBuffer.append(TEXT_173);

        }
        else if (feature instanceof UReference) {
            UReference reference          = (UReference) feature;
            UClass     referenceContainer = reference.getContainingClass();

            String referenceModelName = referenceContainer.getPackage().getName();

            importBuilder.addPackage(
                UGenUtil.getObjectClassPackage(
                    referenceContainer
                )
            );

            String referenceName = reference.getName();
            String literal = UGenUtil.getFeatureLiteral(reference);

        stringBuffer.append(TEXT_174);
        stringBuffer.append(referenceModelName);
        stringBuffer.append(TEXT_175);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_176);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_177);

        }
    }

        stringBuffer.append(TEXT_178);

    for (UFeature feature : object.getAllFeatures()) {
        if (feature instanceof UAttribute) {
            UAttribute attribute          = (UAttribute) feature;
            UClass     attributeContainer = attribute.getContainingClass();

            String attributeModelName = attributeContainer.getPackage().getName();

            importBuilder.addPackage(
                UGenUtil.getObjectClassPackage(
                    attributeContainer
                )
            );

            String attributeName = attribute.getName();
            String literal = UGenUtil.getFeatureLiteral(attribute);

        stringBuffer.append(TEXT_179);
        stringBuffer.append(attributeModelName);
        stringBuffer.append(TEXT_180);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_181);
        stringBuffer.append(UGenUtil.capName(attributeName));
        stringBuffer.append(TEXT_182);

        }
        else if (feature instanceof UReference) {
            UReference reference          = (UReference) feature;
            UClass     referenceContainer = reference.getContainingClass();

            String referenceModelName = referenceContainer.getPackage().getName();

            importBuilder.addPackage(
                UGenUtil.getObjectClassPackage(
                    referenceContainer
                )
            );

            String referenceName = reference.getName();
            String literal = UGenUtil.getFeatureLiteral(reference);

        stringBuffer.append(TEXT_183);
        stringBuffer.append(referenceModelName);
        stringBuffer.append(TEXT_184);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_185);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_186);

        }
    }

        stringBuffer.append(TEXT_187);

    for (UFeature feature : object.getAllFeatures()) {
        if (feature.isChangeable()) {
            if (feature instanceof UAttribute) {
                UAttribute attribute          = (UAttribute) feature;
                UClass     attributeContainer = attribute.getContainingClass();

                String attributeModelName = attributeContainer.getPackage().getName();

                importBuilder.addPackage(
                    UGenUtil.getObjectClassPackage(
                        attributeContainer
                    )
                );

                String attributeName = attribute.getName();
                String attributeType = UGenUtil.getInstanceTypeName(
                    attribute, importBuilder
                );

                String literal = UGenUtil.getFeatureLiteral(attribute);
                String conversionExpression = UGenUtil.createConversionExpressionForType(
                    attribute.getType(), attributeType, "value"
                );

        stringBuffer.append(TEXT_188);
        stringBuffer.append(attributeModelName);
        stringBuffer.append(TEXT_189);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_190);
        stringBuffer.append(UGenUtil.capName(attributeName));
        stringBuffer.append(TEXT_191);
        stringBuffer.append(conversionExpression);
        stringBuffer.append(TEXT_192);

            }
            else if (feature instanceof UReference) {
                UReference reference          = (UReference) feature;
                UClass     referenceContainer = reference.getContainingClass();

                String referenceModelName = referenceContainer.getPackage().getName();

                importBuilder.addPackage(
                    UGenUtil.getObjectClassPackage(
                        referenceContainer
                    )
                );

                String referenceName = reference.getName();
                String referenceType = UGenUtil.getInstanceTypeName(
                    reference, importBuilder
                );

                String literal = UGenUtil.getFeatureLiteral(reference);
                String conversionExpression = UGenUtil.createConversionExpressionForType(
                    reference.getType(), referenceType, "value"
                );

        stringBuffer.append(TEXT_193);
        stringBuffer.append(referenceModelName);
        stringBuffer.append(TEXT_194);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_195);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_196);
        stringBuffer.append(conversionExpression);
        stringBuffer.append(TEXT_197);

            }
        }
    }

        stringBuffer.append(TEXT_198);

    for (UFeature feature : object.getAllFeatures()) {
        if (feature.isChangeable()) {
            if (feature instanceof UAttribute) {
                UAttribute attribute          = (UAttribute) feature;
                UClass     attributeContainer = attribute.getContainingClass();

                String attributeModelName = attributeContainer.getPackage().getName();

                importBuilder.addPackage(
                    UGenUtil.getObjectClassPackage(
                        attributeContainer
                    )
                );

                String attributeName = attribute.getName();
                String attributeType = UGenUtil.getInstanceTypeName(
                    attribute, importBuilder
                );

                String literal = UGenUtil.getFeatureLiteral(attribute);

        stringBuffer.append(TEXT_199);
        stringBuffer.append(attributeModelName);
        stringBuffer.append(TEXT_200);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_201);
        stringBuffer.append(UGenUtil.capName(attributeName));
        stringBuffer.append(TEXT_202);

            }
            else if (feature instanceof UReference) {
                UReference reference          = (UReference) feature;
                UClass     referenceContainer = reference.getContainingClass();

                String referenceModelName = referenceContainer.getPackage().getName();

                importBuilder.addPackage(
                    UGenUtil.getObjectClassPackage(
                        referenceContainer
                    )
                );

                String referenceName = reference.getName();
                String referenceType = UGenUtil.getInstanceTypeName(
                    reference, importBuilder
                );

                String literal = UGenUtil.getFeatureLiteral(reference);

        stringBuffer.append(TEXT_203);
        stringBuffer.append(referenceModelName);
        stringBuffer.append(TEXT_204);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_205);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_206);

            }
        }
    }

        stringBuffer.append(TEXT_207);

    boolean hasBiDirectionalReferences = false;

    for (UReference reference : object.getAllReferences()) {
        UReference opposite  = reference.getOpposite();

        if (opposite != null) {
            hasBiDirectionalReferences = true;
            break;
        }
    }

    if (hasBiDirectionalReferences) {

        stringBuffer.append(TEXT_208);

    for (UReference reference : object.getAllReferences()) {
        UClass referenceContainer = reference.getContainingClass();
        UReference opposite  = reference.getOpposite();

        if (opposite == null) {
            continue;
        }

        String referenceModelName = referenceContainer.getPackage().getName();

        importBuilder.addPackage(
            UGenUtil.getObjectClassPackage(
                referenceContainer
            )
        );

        String referenceName = reference.getName();
        String referenceType = UGenUtil.getInstanceTypeName(
            reference.getType(), importBuilder
        );

        String literal = UGenUtil.getFeatureLiteral(reference);

        stringBuffer.append(TEXT_209);
        stringBuffer.append(referenceModelName);
        stringBuffer.append(TEXT_210);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_211);

            if (reference.isMany()) {
                String getter = "get" + UGenUtil.capName(referenceName);

        stringBuffer.append(TEXT_212);
        stringBuffer.append(TEXT_213);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_214);
        stringBuffer.append(getter);
        stringBuffer.append(TEXT_215);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_216);

            }
            else {

        stringBuffer.append(TEXT_217);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_218);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_219);

            }

        stringBuffer.append(TEXT_220);

    }

        stringBuffer.append(TEXT_221);

    for (UReference reference : object.getAllReferences()) {
        UClass referenceContainer = reference.getContainingClass();
        UReference opposite  = reference.getOpposite();

        if (opposite == null) {
            continue;
        }

        String referenceModelName = referenceContainer.getPackage().getName();

        importBuilder.addPackage(
            UGenUtil.getObjectClassPackage(
                referenceContainer
            )
        );

        String referenceName = reference.getName();
        String referenceType = UGenUtil.getInstanceTypeName(
            reference.getType(), importBuilder
        );

        String literal = UGenUtil.getFeatureLiteral(reference);

        stringBuffer.append(TEXT_222);
        stringBuffer.append(referenceModelName);
        stringBuffer.append(TEXT_223);
        stringBuffer.append(literal);
        stringBuffer.append(TEXT_224);

            if (reference.isMany()) {
                String getter = "get" + UGenUtil.capName(referenceName);

        stringBuffer.append(TEXT_225);
        stringBuffer.append(TEXT_226);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_227);
        stringBuffer.append(getter);
        stringBuffer.append(TEXT_228);
        stringBuffer.append(referenceType);
        stringBuffer.append(TEXT_229);

            }
            else {

        stringBuffer.append(TEXT_230);
        stringBuffer.append(UGenUtil.capName(referenceName));
        stringBuffer.append(TEXT_231);

            }

        stringBuffer.append(TEXT_232);

    }

        stringBuffer.append(TEXT_233);

    }


        stringBuffer.append(TEXT_234);

    String opClassName = UGenUtil.getObjectOpClassName(object);

    for (UOperation operation : object.getAllOperations()) {
        if (operation.isProvided()) {
            continue;
        }

        String signature = UGenUtil.createOperationSignature(
                operation, importBuilder, false, false, "", "public"
        );

        boolean retunAble = operation.getType() != UModel.UVOID;

        stringBuffer.append(TEXT_235);
        stringBuffer.append(signature);
        stringBuffer.append(TEXT_236);
        stringBuffer.append(retunAble ? "return " : "");
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_237);
        stringBuffer.append(operation.getName());
        stringBuffer.append(TEXT_238);
        stringBuffer.append(UGenUtil.createParameterNameListForDelegating("this", operation));
        stringBuffer.append(TEXT_239);

      }

        stringBuffer.append(TEXT_240);
        stringBuffer.append(object.getName());
        stringBuffer.append(TEXT_241);

    for (Iterator<UAttribute> it = object.getAllAttributes().iterator(); it.hasNext();) {
        UAttribute attribute = it.next();

        if (attribute.isDerived() || attribute.isVolatile()) {
            continue;
        }

        String attributeName = attribute.getName();
        String varName       = "_" + attributeName + "_";

        stringBuffer.append(TEXT_242);
        stringBuffer.append(attributeName);
        stringBuffer.append(TEXT_243);
        stringBuffer.append(varName);
        stringBuffer.append(TEXT_244);

        if (it.hasNext()) {

        stringBuffer.append(TEXT_245);

        }
    }

        stringBuffer.append(TEXT_246);

    insertImport(stringBuffer, importBuilder, implPackage);

        stringBuffer.append(TEXT_247);

        return stringBuffer.toString();
    }

    // ---------------------------------------------------------------------------------------
    // ---------------------------------------------------------------------------------------

}
